package com.mn.c14

import android.Manifest
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.hardware.display.DisplayManager
import android.media.MediaRouter
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.Display
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.mn.c14.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.aaa.setOnClickListener {
            mediaRouter()
        }


        binding.bbb.setOnClickListener {
            displayActivity()
        }

        reqManageOverlayPermission()

        val permission = Manifest.permission.SYSTEM_ALERT_WINDOW
        val granted = PackageManager.PERMISSION_GRANTED
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(permission) != granted) {
            requestPermissions(arrayOf(permission), 1000)
        }
    }


    private var mediaRouter: MediaRouter? = null

    private fun mediaRouter() {
        mediaRouter = getSystemService(Context.MEDIA_ROUTER_SERVICE) as MediaRouter
        val info: MediaRouter.RouteInfo =
            mediaRouter?.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO) ?: return

        val display = info.presentationDisplay ?: return
        showPresent(display)
    }

    private fun showPresent(display: Display) {
        val p = MyPresenter(applicationContext, display)
        p.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
        p.show()

    }


    private fun reqManageOverlayPermission(): Boolean {
        return if (!Settings.canDrawOverlays(application)) {
            val intent =
                Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            false
        } else {
            true
        }
    }


    private fun displayActivity() {
        val dm = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
        val ds = dm.displays
        if (ds != null && ds.size > 1) {
//            val secondIntent = Intent(this, SecondActivity::class.java)
//            val options=Bundle()
//            options.putInt("android.activity.launchDisplayId",ds[1].displayId)
//            secondIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
//            startActivity(secondIntent,options)


//            val secondIntent = Intent(this, SecondActivity::class.java)
//            secondIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//            val options = ActivityOptions.makeBasic()
//            options.setLaunchDisplayId(ds[1].displayId)
//
//            startActivity(secondIntent, options.toBundle())

//            val secondIntent = Intent(this, SecondActivity::class.java)
//            secondIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//            secondIntent.putExtra("isInMultiWindowMode", true) // 模拟分屏模式
//
//            startActivity(secondIntent)



        }
    }

}